#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>





void deplacer(int up,int down,int right,int left,int *p,int *pos1,int *pos2,SDL_Surface* hero[12],SDL_Surface* ecran)
{
SDL_Rect phero;
phero.x=(*pos1);
phero.y=(*pos2);
int i=(*p);
        if( down ){

           phero.y+= 16; // Vitesse déplacement droite
                i++;

             if(i>=11)
              {
                (i)=5;
              }

          }

if( left ) {

           phero.x -= 16; // Vitesse déplacement droite
                i--;

             if(i<=0)
              {
                (i)=6;
              }

          }
      else if( right ) {
  phero.x+=16;    //-= 8; // Vitesse déplacement gauche
               i++;
             if(i>=11)
              {
                i=6;
              }
           }

           if( up ) {

           phero.y -= 16; // Vitesse déplacement droite
                i--;

             if(i<=0)
              {
                (i)=5;
              }

          }


(*pos1)=phero.x;
(*pos2)=phero.y;
*p=i;

}
/*void jump(int haut,int *pos2)
{
    double g=9.8;
  for(t=0;t<2000;t+=20)
  {

  }

}*/
void dep_bar_h(SDL_Surface *bar,int*k,int *pos1)
{


if(*k==0){
    (*pos1)+=20;
}
    if((*pos1)==1100){*k=1;}
    if((*pos1)==0){*k=0;}
    if(*k==1){(*pos1)-=20;}
 }

void dep_en_h(SDL_Surface *bar,int*k,int *pos1)
{


if(*k==0){
    (*pos1)+=10;
}
    if((*pos1)==300){*k=1;}
    if((*pos1)==0){*k=0;}
    if(*k==1){(*pos1)-=10;}
 }







int main( int argc, char *argv[ ] )
{
//INITIALISATION DE LA SDL//////////////////////////////
SDL_Init(SDL_INIT_VIDEO);
///////////////////////////////////////////////////////FIN



//DECLARATION DES VARIABLES GLOBALES//////////////////
int fin=0;
SDL_Event event;
int pos1,pos2;
int p=6;
int right=0;
int up=0;
int down=0;
int left=0;
int mur_droit=0;
int mur_gauche=0;
int jump=0,power;
int k=0;
int l=0;
int xbar;
int xsweet;
int tomber=0;
int v=0;
int c=1;
int compteur=10;
int touche_sol=1;
int score=0;
int super_power=0;
int fire=0;
//////////////////////////////////////////////////////FIN




//DECLARATION DES SURFACES//////////////////////////////
    SDL_Surface *ecran,*fond,*hero[12],*bar,*sweet[10],*bonus[10],*bal;
/////////////////////////////////////////////////////// FIN


//CHARGEMENT DE LA FENETRE/////////////////////////////
 ecran = SDL_SetVideoMode(1350,750,32, SDL_HWSURFACE|SDL_FULLSCREEN);
 SDL_WM_SetCaption("Chargement d'images en SDL", NULL);
///////////////////////////////////////////////////////FIN






//CHARGEMENT DES IMAGES///////////////////////////////
fond = SDL_LoadBMP("k7.bmp");
hero[0] = SDL_LoadBMP("left6.bmp");
hero[1] = SDL_LoadBMP("left5.bmp");
hero[2] = SDL_LoadBMP("left4.bmp");
hero[3] = SDL_LoadBMP("left3.bmp");
hero[4] = SDL_LoadBMP("left2.bmp");
hero[5] = SDL_LoadBMP("left1.bmp");
hero[6] = SDL_LoadBMP("right1.bmp");
hero[7] = SDL_LoadBMP("right2.bmp");
hero[8] = SDL_LoadBMP("right3.bmp");
hero[9] = SDL_LoadBMP("right4.bmp");
hero[10] = SDL_LoadBMP("right5.bmp");
hero[11] = SDL_LoadBMP("right6.bmp");
bar = SDL_LoadBMP("mp3.bmp");
sweet[0] = SDL_LoadBMP("s4.bmp");
sweet[1] = SDL_LoadBMP("s4.bmp");
sweet[2] = SDL_LoadBMP("s4.bmp");
bal = SDL_LoadBMP("bal.bmp");
bonus[0] = SDL_LoadBMP("t.bmp");
bonus[1] = SDL_LoadBMP("t1.bmp");
bonus[2] = SDL_LoadBMP("t2.bmp");
bonus[3] = SDL_LoadBMP("t3.bmp");
bonus[4] = SDL_LoadBMP("t4.bmp");

/////////////////////////////////////////////////////FIN

//POSITIONS INITIALES////////////////////////////////
 SDL_Rect pfond;

    pfond.x = 0;
    pfond.y = 0;

SDL_Rect phero;

    phero.x = 500;
    phero.y = 275;





/////////////////////////////////////////////////////FIN




//TRANSPARANCE/////////////////////////////////////////
int i;
for(i=0;i<12;i++){
SDL_SetColorKey(hero[i], SDL_SRCCOLORKEY, SDL_MapRGB(hero[i]->format, 255,255,255));}

SDL_SetColorKey(sweet[0], SDL_SRCCOLORKEY, SDL_MapRGB(sweet[0]->format, 255,255,255));
SDL_SetColorKey(sweet[1], SDL_SRCCOLORKEY, SDL_MapRGB(sweet[1]->format, 255,255,255));
SDL_SetColorKey(sweet[2], SDL_SRCCOLORKEY, SDL_MapRGB(sweet[2]->format, 255,255,255));
SDL_SetColorKey(bar, SDL_SRCCOLORKEY, SDL_MapRGB(bar->format, 255,255,255));
SDL_SetColorKey(bal, SDL_SRCCOLORKEY, SDL_MapRGB(bal->format, 255,255,255));
SDL_SetColorKey(bonus[0], SDL_SRCCOLORKEY, SDL_MapRGB(bonus[0]->format, 255,255,255));
SDL_SetColorKey(bonus[1], SDL_SRCCOLORKEY, SDL_MapRGB(bonus[1]->format, 255,255,255));
SDL_SetColorKey(bonus[2], SDL_SRCCOLORKEY, SDL_MapRGB(bonus[2]->format, 255,255,255));
SDL_SetColorKey(bonus[3], SDL_SRCCOLORKEY, SDL_MapRGB(bonus[3]->format, 255,255,255));
SDL_SetColorKey(bonus[4], SDL_SRCCOLORKEY, SDL_MapRGB(bonus[4]->format, 255,255,255));

//////////////////////////////////////////////////////FIN
SDL_Rect pcam;
pcam.x=100;
pcam.y=1250;
pcam.w=1350;
pcam.h=750;
SDL_Rect pbar;
    pbar.x=2373-pcam.x;
    pbar.y=1765-pcam.y;
    SDL_Rect psweet;
    psweet.x=700-pcam.x;
    psweet.y=1700-pcam.y;
     SDL_Rect psweet1;
    psweet1.x=1000-pcam.x;
    psweet1.y=1700-pcam.y;
      SDL_Rect psweet2;
    psweet2.x=1750-pcam.x;
    psweet2.y=900-pcam.y;
SDL_Rect pbarrel;
    pbarrel.x=0;
    pbarrel.y=0;
     SDL_Rect psweetrel;
      psweetrel.x=0;
   /*    SDL_Rect pbal;
    pbal.x=300-pcam.x;
    pbal.y=1600-pcam.y;*/


      SDL_Rect pbonus;
    pbonus.x=2200-pcam.x;
    pbonus.y=1650-pcam.y;

   SDL_Rect pbonus1;
    pbonus1.x=4350-pcam.x;
    pbonus1.y=1380-pcam.y;

     SDL_Rect pbonus2;
    pbonus2.x=3000-pcam.x;
    pbonus2.y=1000-pcam.y;

     SDL_Rect pbonus3;
    pbonus3.x=1600-pcam.x;
    pbonus3.y=1000-pcam.y;

     SDL_Rect pbonus4;
    pbonus4.x=1500-pcam.x;
    pbonus4.y=900-pcam.y;










//AFFICHAGE DES IMAGES///////////////////////////////
SDL_BlitSurface(fond, &pcam, ecran, NULL);
SDL_BlitSurface(bar, NULL, ecran, &pbar);
SDL_BlitSurface(sweet[0], NULL, ecran, &psweet);
SDL_BlitSurface(sweet[1], NULL, ecran, &psweet1);
SDL_BlitSurface(sweet[2], NULL, ecran, &psweet2);

SDL_BlitSurface(hero[6], NULL, ecran, &phero);
//SDL_BlitSurface(bal, NULL, ecran, &pbal);
SDL_BlitSurface(bonus[0], NULL, ecran, &pbonus);
SDL_BlitSurface(bonus[1], NULL, ecran, &pbonus1);
SDL_BlitSurface(bonus[2], NULL, ecran, &pbonus2);
SDL_BlitSurface(bonus[3], NULL, ecran, &pbonus3);
SDL_BlitSurface(bonus[4], NULL, ecran, &pbonus4);



SDL_Flip(ecran);
////////////////////////////////////////////////////FIN



//KEY REPEAT////////////////////////////////////////
SDL_EnableKeyRepeat(40,40);
///////////////////////////////////////////////////FIN









//BOUCLE PRINCIPALE/////////////////////////////////
  while (!fin)
    {
        // Gestion des evenements

        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                // Quand on ferme la fenetre
            case SDL_QUIT:
                fin = 1;
                break;
            case SDL_KEYDOWN:

                    if (event.key.keysym.sym == SDLK_ESCAPE)
                    {
                      fin=1;
                    }

            }
            switch (event.type)
            {

            case SDL_KEYDOWN:
                {
                    if (event.key.keysym.sym == SDLK_RIGHT)
                    {
                      right=1;
                    }
                    if (event.key.keysym.sym == SDLK_LEFT)
                    {
                      left=1;
                    }
                    if (event.key.keysym.sym == SDLK_UP)
                    {
                      up=1;
                    }
                    if (event.key.keysym.sym == SDLK_DOWN)
                    {
                      down=1;
                    }
                     if (event.key.keysym.sym == SDLK_SPACE)
                    {
                      jump=1;
                    }
                     if (event.key.keysym.sym == SDLK_f)
                    {
                      fire=1;
                    }


                    }
                break;
                 case SDL_KEYUP:
                {
                    if (event.key.keysym.sym == SDLK_RIGHT)
                    {
                      right=0;
                    }
                    if (event.key.keysym.sym == SDLK_LEFT)
                    {
                      left=0;
                    }
                     if (event.key.keysym.sym == SDLK_UP)
                    {
                      up=0;
                    }
                    if (event.key.keysym.sym == SDLK_DOWN)
                    {
                      down=0;
                    }
                    if (event.key.keysym.sym == SDLK_SPACE)
                    {
                      jump=0;
                    }
                      if (event.key.keysym.sym == SDLK_f)
                    {
                      fire=0;
                    }


                }break;
            }

//FONCTION DEPLACER///////////////////////////////////////////

        }

      pos1=pcam.x;
      pos2=pcam.y;
deplacer( up,down,right, left, &p,&pos1,&pos2,&hero[12],ecran);
pcam.x=pos1;
pcam.y=pos2;




//////////////////////////////////////////////////////////






//DEPLACEMENT BAR/////////////////
xbar=pbarrel.x;
dep_bar_h(bar,&k,&xbar);
pbarrel.x=xbar;
pbar.x=2373-pcam.x+pbarrel.x;
pbar.y=1765-pcam.y;
 //////////////////////////////////FIN


 //DEPLACEMENT SWEET/////////////////
xsweet=psweetrel.x;
dep_en_h(sweet[0],&l,&xsweet);
psweetrel.x=xsweet;
psweet.x=700-pcam.x+psweetrel.x;
psweet.y=1700-pcam.y;


xsweet=psweetrel.x;
dep_en_h(sweet[1],&l,&xsweet);
psweetrel.x=xsweet;
psweet1.x=1700-pcam.x+psweetrel.x;
psweet1.y=1700-pcam.y;

xsweet=psweetrel.x;
dep_en_h(sweet[2],&l,&xsweet);
psweetrel.x=xsweet;
psweet2.x=1750-pcam.x+psweetrel.x;
psweet2.y=900-pcam.y;



 //////////////////////////////////FIN
 if(((phero.x)>=psweet.x)&&((phero.x)<=(psweet.x+50))&&(phero.y>=psweet.y-200  ))
 {
     hero[0]=NULL;
     hero[1]=NULL;
     hero[2]=NULL;
     hero[3]=NULL;
     hero[4]=NULL;
     hero[5]=NULL;
     hero[6]=NULL;
     hero[7]=NULL;
     hero[8]=NULL;
     hero[9]=NULL;
     hero[10]=NULL;
     hero[11]=NULL;
 }






//SUR BAAAAAAAAAAAAAAAAAAAAAAAAAAAAR//////////
int x,y;
 x=pbar.x-200;


if((phero.x>=pbar.x)&&(phero.x<pbar.x+473)&&(phero.y>=pbar.y-280)&&(!up)&&(!left)&&(!right))
    {
      //phero.x=pbar.x;

        pcam.x=x+pcam.x-125;

}
/////////////////////////////////////////////FIN

//CHUTEE//////////////////////////////////////////
if((phero.x>=250-pcam.x)&&(phero.x<=1225-pcam.x)&&(pcam.y>1200) )
{
    tomber=0;
}

if((phero.x>=1225-pcam.x)&&(phero.x<=1350-pcam.x-50)&&(pcam.y>1200) )
{
    tomber=1;
}
if((phero.x>=4150-pcam.x)&&(phero.x<=4350-pcam.x-50)&&(pcam.y>720)&&(pcam.y<740) )
{
    tomber=0;touche_sol=0;
}
if((phero.x>=3900-pcam.x)&&(phero.x<=4420-pcam.x)&&(pcam.y>=980)&&(pcam.y<=1000))
{
    touche_sol=0;tomber=0;
}
if((phero.x>=3800-pcam.x)&&(phero.x<=3980-pcam.x)&&(pcam.y>=600)&&(pcam.y<=610))
{
    touche_sol=0;tomber=0;
}
if((phero.x>=2640-pcam.x)&&(phero.x<=3605-pcam.x)&&(pcam.y>=800)&&(pcam.y<=810))
{
    touche_sol=0;tomber=0;
}
if((phero.x>=1600-pcam.x)&&(phero.x<=2600-pcam.x)&&(pcam.y>=450)&&(pcam.y<=460))
{
    touche_sol=0;tomber=0;
}
if((phero.x>=150-pcam.x)&&(phero.x<=1200-pcam.x)&&(pcam.y>=390)&&(pcam.y<=400))
{
    touche_sol=0;tomber=0;
}


if((phero.x>=2700-pcam.x)&&(phero.x<=2900-pcam.x)&&(pcam.y>=600)&&(pcam.y<=610))
{
    touche_sol=0;tomber=0;
}

if((phero.x>=1400-pcam.x)&&(phero.x<=1550-pcam.x)&&(pcam.y>=250)&&(pcam.y<=260))
{
    touche_sol=0;tomber=0;
}


if((phero.x>=770-pcam.x)&&(phero.x<=870-pcam.x)&&(pcam.y>=250)&&(pcam.y<=260))
{
    touche_sol=0;tomber=0;
}

if((phero.x>=380-pcam.x)&&(phero.x<=590-pcam.x)&&(pcam.y>=67)&&(pcam.y<=77))
{
    touche_sol=0;tomber=0;
}

if((phero.x>=2880-pcam.x)&&(phero.x<=3050-pcam.x)&&(pcam.y>=270)&&(pcam.y<=280))
{
    touche_sol=0;tomber=0;
}

if((phero.x>=3350-pcam.x)&&(phero.x<=3590-pcam.x)&&(pcam.y>=140)&&(pcam.y<=150))
{
    touche_sol=0;tomber=0;
}

if((phero.x>=3820-pcam.x)&&(pcam.y>=50)&&(pcam.y<=60))
{
    touche_sol=0;tomber=0;
}



if((phero.x>=2350-pcam.x)&&(phero.x<pbar.x)&&(pcam.y>1200))
{
    tomber=1;
}
if((phero.x>pbar.x+473)&&(pcam.y>1200))
{
    tomber=1;
}







if((tomber==1)/*&&(v==1)*/)
{
    phero.y+=20;
}
///////////////////////////////////////////////////FIN
if(pcam.y>1220)
{
    touche_sol=0;
}







if((jump)||(v==1))
{
    if(c==1)
    {pcam.y-=20;
    compteur++;}
    if(c==0)
    {pcam.y+=20;}
    if(compteur>=15)
    {
        c=0;
    }
    v=1;
    }



if(touche_sol==0){v=0;compteur=0;c=1;;touche_sol=1;}



SDL_Rect pbal;

   if(fire==0)
   {
    pbal.x=300-pcam.x;
    pbal.y=1650-pcam.y;
   }

       SDL_Rect pbonus;
    pbonus.x=2200-pcam.x;
    pbonus.y=1650-pcam.y;
    if((phero.x>=pbonus.x)&&(phero.x<=pbonus.x+200)&&(phero.y>=pbonus.y-130)&&(phero.y<=pbonus.y)&&(bonus[0]!=NULL))
    {
        bonus[0]=NULL;
        score++;
    }




   SDL_Rect pbonus1;
    pbonus1.x=4350-pcam.x;
    pbonus1.y=1360-pcam.y;

     if((phero.x>=pbonus1.x)&&(phero.x<=pbonus1.x+130)&&(phero.y>=pbonus1.y-130)&&(phero.y<=pbonus1.y)&&(bonus[1]!=NULL))
    {
        bonus[1]=NULL;
        score++;
    }

      SDL_Rect pbonus2;
    pbonus2.x=3000-pcam.x;
    pbonus2.y=1250-pcam.y;
      if((phero.x>=pbonus2.x)&&(phero.x<=pbonus2.x+130)&&(phero.y>=pbonus2.y-150)&&(phero.y<=pbonus2.y)&&(bonus[2]!=NULL))
    {
        bonus[2]=NULL;
        score++;
    }

     SDL_Rect pbonus3;
    pbonus3.x=500-pcam.x;
    pbonus3.y=475-pcam.y;
    if((phero.x>=pbonus3.x)&&(phero.x<=pbonus3.x+130)&&(phero.y>=pbonus3.y-150)&&(phero.y<=pbonus3.y)&&(bonus[3]!=NULL))
    {
        bonus[3]=NULL;
        score++;
    }

     SDL_Rect pbonus4;


    pbonus4.x=1670-pcam.x;
    pbonus4.y=880-pcam.y;

    if((score==4)&&(phero.x>=pbal.x)&&(phero.x<=pbal.x+130)&&(phero.y>=pbal.y-150)&&(phero.y<=pbal.y))
    {


        super_power=1;
    }
    if(super_power)
    {

         if(fire==1)
   {
       pbal.x+=50;}



if((pbal.x<500)&&(fire==0))
    {
        pbal.x=phero.x;
        pbal.y=phero.y;
}
    }







SDL_Delay(30);
SDL_BlitSurface(fond,&pcam, ecran, NULL);
SDL_BlitSurface(bar, NULL, ecran, &pbar);
SDL_BlitSurface(bal, NULL, ecran, &pbal);
SDL_BlitSurface(sweet[0], NULL, ecran, &psweet);
SDL_BlitSurface(sweet[1], NULL, ecran, &psweet1);
SDL_BlitSurface(sweet[2], NULL, ecran, &psweet2);
SDL_BlitSurface(hero[p],NULL,ecran,&phero);

SDL_BlitSurface(bonus[0], NULL, ecran, &pbonus);
SDL_BlitSurface(bonus[1], NULL, ecran, &pbonus1);
SDL_BlitSurface(bonus[2], NULL, ecran, &pbonus2);
SDL_BlitSurface(bonus[3], NULL, ecran, &pbonus3);
SDL_BlitSurface(bonus[4], NULL, ecran, &pbonus4);
SDL_Flip(ecran);



////////////////////////////////////////////////////FIN

SDL_Flip(ecran);
    }
///////////////////////////////////////////////////FIN:BOUCLE PRINCIPALE















    SDL_Quit();
    return EXIT_SUCCESS;
}
